﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//



using System;
using System.Collections.Generic;

namespace KretschIT.WP_Fx.EasyMoq.Generator.Usings
{
    internal class UsingsBuilder : IUsingsBuilder
    {
        private List<string> usings = new List<string>();

        public IEnumerable<string> Usings
        {
            get { return this.usings; }
        }

        public void Add(Type type)
        {
            if (type == null)
            {
                return;
            }
            var name = type.Namespace;
            this.Add(name);
        }

        private void Add(string name)
        {
            if (usings.Contains(name))
            {
                return;
            }
            usings.Add(name);
        }

        public void Add(IEnumerable<Type> types)
        {
            if (types == null)
            {
                return;
            }
            this.AddTypes(types);
        }

        private void AddTypes(IEnumerable<Type> types)
        {
            foreach (var type in types)
            {
                this.Add(type);
            }
        }

        public void Add(IEnumerable<string> namespaces)
        {
            if (namespaces == null)
            {
                return;
            }
            this.AddNamespaces(namespaces);
        }

        private void AddNamespaces(IEnumerable<string> namespaces)
        {
            foreach (var n in namespaces)
            {
                this.Add(n);
            }
        }

        public void Merge(IUsingsBuilder usingsBuilder)
        {
            if (usingsBuilder == null)
            {
                return;
            }
            this.MergeUsings(usingsBuilder);
        }

        private void MergeUsings(IUsingsBuilder usingsBuilder)
        {
            foreach (var name in usingsBuilder.Usings)
            {
                this.Add(name);
            }
        }

        public void Remove(IUsingsBuilder usingsBuilder)
        {
            if (usingsBuilder == null)
            {
                return;
            }
            this.RemoveUsings(usingsBuilder);
        }

        private void RemoveUsings(IUsingsBuilder usingsBuilder)
        {
            foreach (var n in usingsBuilder.Usings)
            {
                if (this.usings.Contains(n))
                {
                    this.usings.Remove(n);
                }
            }
        }
    }
}
